// src/common/interceptors/response.interceptor.ts
import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
  HttpStatus,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable()
export class ResponseInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(
      map((data) => {
        // 检查是否存在自定义消息
        const customMessage = context.switchToHttp().getRequest().customMessage;
        const response = context.switchToHttp().getResponse();
        if (response.statusCode  === 201) {
          response.status(HttpStatus.OK);
        }
        return {
          errno: 0,
          message: customMessage || 'success',
          data,
        };
      }),
    );
  }
}
